package ks.server.controllers.commands.general;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.framework.debug.Debug;
import ks.framework.interfaces.IProcessMessage;
import ks.managers.user.User;
import ks.server.persist.PersistentUserManager;
import ks.server.ServerContext;

import org.w3c.dom.Document;

/**
 * Handle the login command for users.
 * 
 * @author George Heineman
 */
public class LoginController implements IProcessMessage {

	/** Primary agent to whom we can communicate our messages. */
	Communicator com;
	
	public LoginController (Communicator com) {
		this.com = com;
	}
	
	private static int ctr = 0;
	
	/**
	 * Properly initialize the user by sending all initial pieces of information
	 * along to the user.
	 * 
	 * At the same time, we must tell everyone else on the system that we are now
	 * connected.
	 */
	public boolean process(Message m) {
		Debug.println ("Observe login by: " + m.getOriginator());
		
		// CONNECTING request.
//		<request version="1.0" id="589a39591271844e3fbe32bbb9281ad9">
//		<connecting>
//		<player name="george" rating="1992" icon="stylish"/>
//		<player name="admin" rating="1833" icon="ghoulish"/>
//		</connecting>
//		</request>

		String loggingInUser = m.getOriginator();
		PersistentUserManager uman = ServerContext.getUserManager();		
		User u = uman.getUser(loggingInUser);
		
		// send CONNECTING to everyone on the system EXCEPT SELF. 
		
		// produce request to go to the client
        StringBuilder sb = new StringBuilder("<request version='1.0' id='" + (++ctr) + "'>");
        sb.append("<connecting>");
        sb.append("<player name=\"").append(loggingInUser).append("\" rating=\"").append(u.getRating()).append("\" icon=\"").append(u.getIconName()).append("\"/>");
        sb.append("</connecting></request>");
        Document d = Message.construct (sb.toString());
        
        // send to all clients EXCEPT SELF
        Message r = new Message (d);
        r.setOriginator(m.getOriginator());   // this is on our behalf...
        r.setBroadcast();
        com.distribute(r);
        
        // now construct set of connecting users for the logging-in-player which includes
        // all users of the system.
		// produce request to go to the client
        sb = new StringBuilder("<request version='1.0' id='" + (++ctr) + "'>");
        sb.append("<connecting>");
        for (String name : com.getOnlineUsers()) {
        	u = uman.getUser(name);
        	if (u.getName().equalsIgnoreCase(m.getOriginator()) && u.isAdmin())
        		sb.append("<player name=\"" + u.getName() + "\" rating=\"" + u.getRating() + "\" icon=\"" + u.getIconName() + "\" admin=\"" + u.isAdmin() + "\"/>");
        	else
        		sb.append("<player name=\"").append(u.name).append("\" rating=\"").append(u.getRating()).append("\" icon=\"").append(u.getIconName()).append("\"/>");
        }
  
        sb.append("</connecting></request>");
        d = Message.construct (sb.toString());
        
        // send to all clients EXCEPT SELF
        r = new Message (d);
        r.setOriginator(m.getOriginator());   // this is on our behalf...
        r.setRecipient(loggingInUser);
        com.distribute(r);
        
        // done
        return true;
	}
}